package com.itheima.service.mongo.impl;

import cn.hutool.core.collection.CollUtil;
import com.itheima.domain.mongo.UserLocation;
import com.itheima.service.mongo.UserLocationService;
import com.itheima.soul.UserSiteVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Metrics;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;


@DubboService
public class UserLocationServiceImpl implements UserLocationService {

    @Autowired
    MongoTemplate mongoTemplate;

    @Override
    public void save(Double longitude, Double latitude, String addrStr, Long userId) {
        //1.创建条件对象(userId)
        //2.查询userLocation表获取对象
        UserLocation userLocation = mongoTemplate.findOne(new Query(Criteria.where("userId").is(userId)), UserLocation.class);

        //3.判断对象是否为空
        if (userLocation == null) {
            //3.1 若为空,就是保存操作
            userLocation = new UserLocation();
            userLocation.setUserId(userId);
            userLocation.setLocation(new GeoJsonPoint(longitude,latitude));//先写经再写纬
            userLocation.setAddress(addrStr);
            userLocation.setCreated(System.currentTimeMillis());
            userLocation.setUpdated(System.currentTimeMillis());

            mongoTemplate.save(userLocation);
        }else{
            //3.2 若不为空,就是更新操作
            userLocation.setLocation(new GeoJsonPoint(longitude,latitude));//先写经再写纬
            userLocation.setAddress(addrStr);
            //更新"上次更新时间"
            userLocation.setLastUpdated(userLocation.getUpdated());
            userLocation.setUpdated(System.currentTimeMillis());

            mongoTemplate.save(userLocation);
        }
    }

    @Override
    public List<Long> searchNear(Long userId, Long distance) {
        //1.根据用户id搜索用户的位置(圆心)
        GeoJsonPoint point = mongoTemplate.findOne(new Query(Criteria.where("userId").is(userId)), UserLocation.class).getLocation();

        //2.创建条件对象,以用户位置为圆心,距离为半径,搜索范围之内的userLocation的集合
        Distance radius = new Distance(distance/1000, Metrics.KILOMETERS);
        Circle circle = new Circle(point,radius);
        Query query = new Query(Criteria.where("location").withinSphere(circle));

        List<UserLocation> userLocations = mongoTemplate.find(query, UserLocation.class);

        //3.遍历原来集合,获取用户的id放入一个新集合返回即可
        List<Long> ids = new ArrayList<>();
        if (CollUtil.isNotEmpty(userLocations)) {
            for (UserLocation userLocation : userLocations) {
                ids.add(userLocation.getUserId());
            }
        }
        return ids;
    }


    @Override
    public UserSiteVo findByUserId(Long userId) {

        Query query = new Query(Criteria.where("userId").is(userId));


        UserLocation userLocation = mongoTemplate.findOne(query, UserLocation.class);

        if (userLocation==null) {
            userLocation = new UserLocation();

            userLocation.setLocation(new GeoJsonPoint(116.635708,40.169522));
            userLocation.setAddress("顺义黑马程序员");
        }

        GeoJsonPoint location = userLocation.getLocation();
        String address = userLocation.getAddress();

        UserSiteVo userSiteVo = new UserSiteVo();

        userSiteVo.setLongitude(location.getX());
        userSiteVo.setLatitude(location.getY());
        userSiteVo.setAddress(address);




        return userSiteVo;
    }
}
